﻿
using Minio.DataModel;

namespace Admin.NET.Application.Service;

/// <summary>
/// 输血申请标本表
/// </summary>
public class TraBiaoBenService : BaseServiceBill<TraBiaoBen>
{

    public TraBiaoBenService(IServiceScopeFactory scopeFactory) : base(scopeFactory)
    {
    }

    /// <summary>
    /// 得到标本状态
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    private ITraBiaoBenStateManager BiaoBenZhuangTaiNew(TraBiaoBen entity)
    {
        //设置当前状态为绑定
        ITraBiaoBenStateManager _currentState = new TraBangDingStateManager(_rep);

        if (entity == null)
        {
            entity = new TraBiaoBen { danJuZhuangTai = BiaoBenZhuangTai.待绑定 };
        }

        switch (entity.danJuZhuangTai)
        {
            case BiaoBenZhuangTai.待绑定:
                _currentState = new TraDaiBangDingStateManager(_rep);
                break;
            case BiaoBenZhuangTai.已绑定:
                _currentState = new TraBangDingStateManager(_rep);
                break;
            case BiaoBenZhuangTai.已采集:
                _currentState = new TraCaiJiStateManager(_rep);
                break;
            case BiaoBenZhuangTai.已送出:
                _currentState = new TraSongChuStateManager(_rep);
                break;
            case BiaoBenZhuangTai.已送达:
                _currentState = new TraSongDaStateManager(_rep);
                break;
            case BiaoBenZhuangTai.已接收:
                _currentState = new TraJieShouStateManager(_rep);
                break;
            case BiaoBenZhuangTai.已作废:
                _currentState = new TraZuoFeiStateManager(_rep);
                break;
            default:
            case BiaoBenZhuangTai.已销毁:
                _currentState = new TraZuoFeiStateManager(_rep);
                break;

                throw Oops.Oh("目前标本处于无效状态" + entity.danJuZhuangTai);
        }

        return _currentState;
    }






    /// <summary>
    /// 扫码标本进行操作
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<string> ScanBiaoBen(TraBiaoBenSaoMaInput input) {

        var entity = await _rep.AsQueryable().Where(a => a.Id == input.Id).FirstAsync();
        
        //标本状态
        ITraBiaoBenStateManager _currentState = BiaoBenZhuangTaiNew(entity);
        
        //标本操作
        switch (input.saoMaLeiXing)
        {
            case BiaoBenZhuangTai.已绑定:
              await  _currentState.BangDing(input);
                break;
            case BiaoBenZhuangTai.已采集:
                _currentState.CaiJi(input, entity);
                break;
            case BiaoBenZhuangTai.已送出:
                _currentState.SongChu(input, entity);
                break;
            case BiaoBenZhuangTai.已送达:
                _currentState.SongDa(input, entity);
                break;
            case BiaoBenZhuangTai.已接收:
                _currentState.JieShou(input, entity);
                break;
            default:
                break;
        }

        return "";
    }


    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<TraBiaoBenOutput>> Page(BaseKeywordPageInput input)
    {
        return await Queryable
           .WhereIF(input.Keyword.IsNullOrEmpty(), it => it.Id.Contains(input.Keyword))
           .Select<TraBiaoBenOutput>()
           .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public TraBiaoBenOutput Get(string id)
    {
        return GetById<TraBiaoBenOutput>(id);
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public TraBiaoBen Add(TraBiaoBenInput input)
    {
        return AddWithInput(input);
    }

    /// <summary>
    /// 更新成功后返回true
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public bool Update(TraBiaoBenInput input)
    {
        return UpdateWithInput(input);
    }
}
